#ifndef BILIBILIMANAGER_H
#define BILIBILIMANAGER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkRequest>
#include <QTimer>

class BilibiliQRCodeGenerator : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString url READ url NOTIFY urlChanged)
    Q_PROPERTY(QString qrcodeKey READ qrcodeKey NOTIFY qrcodeKeyChanged)
    Q_PROPERTY(bool isLoading READ isLoading NOTIFY isLoadingChanged)
    Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)

public:
    explicit BilibiliQRCodeGenerator(QObject *parent = nullptr);

    // 属性getter
    QString url() const { return m_url; }
    QString qrcodeKey() const { return m_qrcodeKey; }
    bool isLoading() const { return m_isLoading; }
    QString errorMessage() const { return m_errorMessage; }

public slots:
    // 生成二维码
    void generateQRCode();

signals:
    void urlChanged();
    void qrcodeKeyChanged();
    void isLoadingChanged();
    void errorMessageChanged();
    void qrcodeGenerated(bool success, const QString& url, const QString& qrcodeKey);

private slots:
    void onReplyFinished();
    void onNetworkError(QNetworkReply::NetworkError error);

private:
    void setUrl(const QString& url);
    void setQrcodeKey(const QString& key);
    void setIsLoading(bool loading);
    void setErrorMessage(const QString& message);

    QNetworkAccessManager* m_networkManager;
    QNetworkReply* m_currentReply;

    QString m_url;
    QString m_qrcodeKey;
    bool m_isLoading;
    QString m_errorMessage;
};

#endif // BILIBILIMANAGER_H
